<template>
  <section class="cms-body" v-loading="loading">
    <!-- 返回组件 -->
    <cms-back></cms-back>
    <el-form ref="form" :model="dataInfo" :rules="rules" class="cms-form" label-width="162px">

      <el-form-item label="类型名称" class="flex-50" prop="typeName">
        <el-input v-model="dataInfo.typeName" class="cms-width"></el-input>
      </el-form-item>

      <el-form-item label="能否删除：" class="flex-50" prop="isCanDel" v-if="isType('save')">
        <el-radio-group v-model="dataInfo.isCanDel">
          <el-radio :label="true">能</el-radio>
          <el-radio :label="false">不能</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="排序" class="flex-50" prop="goodsTypeSort">
        <el-input v-model="dataInfo.goodsTypeSort" class="cms-width"></el-input>
      </el-form-item>

    </el-form>

    <div class="form-footer">
      <el-button type="warning" v-if="isType('save')" @click="add(true)" v-perms="'/goodsType/save'">提交并继续添加</el-button>
      <el-button type="primary" v-if="isType('save')" @click="add(false)" v-perms="'/goodsType/save'">添加</el-button>
      <el-button type="primary" v-if="isType('update')" @click="update()" v-perms="'/goodsType/update'">修改</el-button>
      <el-button type="info" @click="$reset">重置</el-button>
      <span class="gray" v-if="isType('save')">选择“提交并继续添加”按钮会停留在添加页面；选择“提交”按钮后将跳转到对应的列表页</span>
    </div>

  </section>
</template>
<script>
  import axios from "axios/index";
  import va from "@/rules";
  import formMixns from "@/mixins/form";

  export default {
    mixins: [formMixns],
    data() {
      let required = va.required();
      let number = va.number();
      return {
        rules: {//校验规则
          id: [number],
          name: [required],
          goodsTypeSort: [required, number]
        },
        dataInfo: {
          goodsTypeId: '',
          typeName: '',
          typeCode: '',
          isCanDel: true,
          goodsTypeSort: ''
        }
      };
    },
    methods: {
      getDataInfo(id, type) {
        if (type == 'update') {
          //重写获取表单数据
          let api = this.$api; //API地址
          axios.all([
            axios.post(api.goodsTypeGet, {goodsTypeId: id}),
          ]).then(axios.spread((res) => {
            console.log(res);
            this.loading = false;
            this.dataInfo = res.body; //将用户数据复制给dataInfo
            this.$refs["form"].resetFields();
          })).catch(error => {
            console.log(error);
          });
        }
        this.loading = false;
      },
      add(state) {
        this.saveDataInfo(state, this.$api.goodsTypeSave, this.dataInfo, "pageList");
      },
      update() {
        console.log(this.dataInfo);
        this.updateDataInfo(this.$api.goodsTypeUpdate, this.dataInfo, "pageList");
      },
      handleClose(done) {
        done();
      }
    },
    created() {
      //初始获取数据
      this.getDataInfo(this.id, this.type);
    }
  };
</script>

<style>
</style>
